package com.Lyf.user.config;


import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.HashMap;
import java.util.Map;

import static com.Lyf.user.config.RabbitMQConstant.*;

//延迟队列,死信队列
@Configuration
public class RabbitConfig {

    //交换机这种东西是rabbitMQ内部所特有的
    @Bean("delayExchange")
    public DirectExchange delayExchange() {
        return new DirectExchange(DELAY_EXCHANGE);
    }

    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange(){
        return new DirectExchange(DEAD_LETTER_EXCHANGE);
    }

    @Bean("delayQueueA")
    public Queue delayQueueA(){
        Map<String,Object> args=new HashMap<>();
        //延迟队列连接的死信交换机是哪个
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        //死信队列的死信routing-key是啥(延迟队列到死信队列得使用同一个routingKey,消息才能过去)
        args.put("x-dead-letter-routing-key",DEAD_LETTER_EXCHANGE_TO_DEAD_LETTER_QUEUE_A_ROUTING_KEY);
        //延迟队列中消息的存活时间是几秒(ms)
        args.put("x-message-ttl",10000);
        return QueueBuilder.durable(DELAY_QUEUE_A).withArguments(args).build();
    }

    @Bean("delayQueueB")
    public Queue delayQueueB(){
        Map<String,Object> args=new HashMap<>();
        args.put("x-dead-letter-exchange",DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key",DEAD_LETTER_EXCHANGE_TO_DEAD_LETTER_QUEUE_B_ROUTING_KEY);
        args.put("x-message-ttl",15000);
        return QueueBuilder.durable(DELAY_QUEUE_B).withArguments(args).build();
    }

    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA(){
        return new Queue(DEAD_LETTER_QUEUE_A);
    }
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB(){
        return new Queue(DEAD_LETTER_QUEUE_B);
    }

    //延迟交换机与延迟队列绑定
    @Bean
    public Binding bindingDelaydQueueA(@Qualifier("delayQueueA")Queue delayQueueA,@Qualifier("delayExchange")DirectExchange delayExchange){
        return BindingBuilder.bind(delayQueueA).to(delayExchange).with(DELAY_EXCHANGE_TO_DELAY_QUEUE_A_ROUTING_KEY);
    }
    @Bean
    public Binding bindingDelayQueueB(@Qualifier("delayQueueB")Queue delayQueueB,@Qualifier("delayExchange")DirectExchange delayExchange){
        return BindingBuilder.bind(delayQueueB).to(delayExchange).with(DELAY_EXCHANGE_TO_DELAY_QUEUE_B_ROUTING_KEY);
    }

    @Bean
    public Binding bindingDeadLetterQueueA(@Qualifier("deadLetterQueueA")Queue queue,@Qualifier("deadLetterExchange")DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue).to(deadLetterExchange).with(DEAD_LETTER_EXCHANGE_TO_DEAD_LETTER_QUEUE_A_ROUTING_KEY);
    }
    @Bean
    public Binding bindingDLetterQueueB(@Qualifier("deadLetterQueueB")Queue queue,@Qualifier("deadLetterExchange")DirectExchange deadLetterExchange){
        return BindingBuilder.bind(queue).to(deadLetterExchange).with(DEAD_LETTER_EXCHANGE_TO_DEAD_LETTER_QUEUE_B_ROUTING_KEY);
    }
}
